package com.ltmall.api.parse;

import com.ltmall.api.bean.ActivityPrice;
import com.ltmall.api.bean.ThSku;
import com.ltmall.api.comm.Path;
import com.ltmall.api.comm.PathUtil;
import com.ltmall.api.entity.JsonCart;
import com.ltmall.api.entity.JsonShop;
import com.ltmall.api.manager.CartManager;
import com.ltmall.api.manager.GoodsManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CartParse {
    //商品图片的路径 一个cart代表什么？应该在这个地方进行判断，判断每张单据的商家信息，重新处理后，返回给用户
    static String goodsPath = Path.nginxServerBusinessDir + Path.GOODS + File.separator;

    public static List<JsonCart> parse(GoodsManager gimpl, CartManager impl, int userID, int shopID, boolean newVersionForWM) {

        List<?> list = impl.cartList(userID, shopID);
        List<JsonCart> cartList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Object[] obj = (Object[]) list.get(i);
            JsonCart cart = new JsonCart();

            cart.setCartID(Integer.parseInt(obj[0].toString()));
            cart.setGoodsID(Integer.parseInt(obj[1].toString()));
            //如果是两个商品怎么办？
            cart.setGoodsName(obj[2].toString());

            cart.setGoodsPicture(getGoodsPicPath(obj[3].toString()));
            cart.setGoodsType(Integer.parseInt(obj[4].toString()));
            cart.setGoodsNumber(Integer.parseInt(obj[5].toString()));
            cart.setGoodsFormat(obj[6].toString());

            /*
             * 先判断当前商品是否存在是活动商品，如果是
             */
            if (newVersionForWM == false) {
                //老接口
                ActivityPrice ap = gimpl.getActivityPrice(Integer.parseInt(obj[1].toString()), obj[6].toString());
                if (ap != null) {
                    cart.setGoodsPrice(ap.getStartPrice());
                } else {
                    ThSku p = getGoodsPrice(impl, Integer.parseInt(obj[1].toString()), obj[6].toString());
                    if (p == null) {
                        continue;
                    }
                    cart.setGoodsPrice(p.getPrice());
                }
            } else {
                // 新接口
                //活动id
                int activityID = Integer.parseInt(obj[7].toString());
                //活动名称
                String activityName = obj[8].toString();
                //这是活动商品
                if (activityID > 0) {
                    ActivityPrice ap = gimpl.getActivityPrice(activityID, Integer.parseInt(obj[1].toString()), obj[6].toString());
                    if (ap != null) {
                        cart.setGoodsPrice(ap.getStartPrice());
                        cart.setActivityID(activityID);
                        cart.setActivityName(activityName);
                    } else {
                        ThSku p = getGoodsPrice(impl, Integer.parseInt(obj[1].toString()), obj[6].toString());
                        if (p == null) {
                            continue;
                        }
                        cart.setGoodsPrice(p.getPrice());
                    }
                } else {
                    ThSku p = getGoodsPrice(impl, Integer.parseInt(obj[1].toString()), obj[6].toString());
                    if (p == null) {
                        continue;
                    }
                    cart.setGoodsPrice(p.getPrice());
                }
            }

            cartList.add(cart);
        }
        return cartList;
    }


    //获取商品价格
    public static ThSku getGoodsPrice(CartManager impl, int goodsID, String format) {
        ThSku price = impl.goodsPrice(goodsID, format);
        return price;
    }

    public static List<JsonShop> shopParse(List<?> list, int userID, CartManager impl, GoodsManager gimpl, boolean newVersionForWM) {
        List<JsonShop> shopList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Object[] obj = (Object[]) list.get(i);

            JsonShop shop = new JsonShop();
            shop.setShopID(Integer.parseInt(obj[0].toString()));
            shop.setShopName(obj[1].toString());

            List<JsonCart> parseCarts = parse(gimpl, impl, userID, Integer.parseInt(obj[0].toString()), newVersionForWM);
            if (parseCarts.size() != 0) {
                shop.setCartList(parseCarts);
                shopList.add(shop);
            }
//			shop.setCartList(parse(impl, userID, Integer.parseInt(obj[0].toString())));
//			shopList.add(shop);
        }
        return shopList;
    }

    public static String getGoodsPicPath(String picPaths) {
        if (picPaths == null) {
            return "";
        }

        return PathUtil.getPicPath(picPaths, Path.GOODS);
    }

}
